package Sort;

import java.util.Arrays;

/**
 * 作者：吴超
 * 邮箱：wuchao_19931120@163.com
 * 时间：2022-01-18
 * 描述：
 */

public class QuickSort {

    public static void quick(int[] arr, int left, int right){

        if(arr == null || arr.length ==0){
            return;
        }
        if(left > right){
            return;
        }

        int key = arr[left];
        int l = left;
        int r = right;
        while (l != r){
            while (arr[r] >= key && l < r){
                r--;
            }
            while (arr[l] <= key && l < r){
                l++;
            }
            if(l < r){
                int tmp = arr[l];
                arr[l] = arr[r];
                arr[r] = tmp;
            }
        }
        arr[left] = arr[l];
        arr[l] = key;
        quick(arr,left,l-1);
        quick(arr,l+1,right);
    }

    public static void main(String[] args) {

        int[] numbers = new int[]{12, 4, 34, 1, 5, 7, 8, 6};
        quick(numbers,0,numbers.length - 1);
        for (int a : numbers) {
            System.out.println(a);
        }
    }

}
